<template>
  <div>
    <el-container class="home-container">
      <!-- 顶部菜单栏 -->
      <el-header>
        <div>
          <img src="../assets/logo2.png" alt="logo">
          <span>Vue-Eshop-Admin</span>
        </div>
        <el-button class="exit-btn" @click="exit">退出</el-button>
      </el-header>
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="toggleButtonClick">|||</div>
          <!-- 侧边菜单 -->
          <el-menu :router="true" :collapse="isCollapse" :unique-opened="true"
          background-color="#C2CADD" text-color="black" active-text-color="#965456"
          :default-active="activePath">

            <!-- 一级菜单 -->
            <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
              <!-- 一级菜单的模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconsObj[item.id]"></i>
                <!-- 文本 -->
                <span>{{item.authName}}</span>
              </template>

              <!-- 二级菜单 -->
              <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children"
              :key="subItem.id" @click="saveNavState('/' + subItem.path)">
                <!-- 一级菜单的模板 -->
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧内容主体 -->
        <el-main>
          <!-- 子路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconsObj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-s-cooperation',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    // 一创建Home组件就获取被激活的菜单栏
    this.activePath = sessionStorage.getItem('activePath')
  },
  methods: {
    exit () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('/menus')
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 点击按钮切换菜单的折叠和展开
    toggleButtonClick () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang='less' scoped>
.home-container{
  // height: 100%;
  height: 100vh;
}
.el-header{
  background-color: #8A95A7;
  display: flex;
  justify-content: space-between;
  padding-left: 5px;
  align-items: center;
  font-size: 20px;
  color: #eee;
  img {
    max-width: 100%;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background-color: #eee;
  }
  // 嵌套样式
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside{
  background-color: #C2CADD;
  .el-menu{
    border-right: none;
  }
}
.el-main{
  background-color: #EAEDF1;
}
.exit-btn{
  color: black;
  background-color: #E2E4E1;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button{
  background-color: #DDD9DA;
  font-size: 10px;
  line-height: 24px;
  color: white;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
